package com.venus.mq.storage;

import java.nio.ByteBuffer;

/**
 * Created by qts on 2018/12/10.
 */
public class TopicFileService extends TopicFile {

    /**
     * 用于添加topic文件
     * @param data
     * @return
     */
    public boolean appendTopic(final byte[] data) {
        int currentPos = this.wrotePosition.get();

        if ((currentPos + data.length) <= this.fileSize) {
            try {
                this.fileChannel.position(currentPos);
                this.fileChannel.write(ByteBuffer.wrap(data));
            } catch (Throwable e) {
                log.error("Error occurred when append message to mappedFile.", e);
            }
            this.wrotePosition.addAndGet(data.length);
            return true;
        }
        return false;
    }

}
